#include "common.h"
#include "lmodel.h"
#include "shadow.h"

//////////////////////////////////////////////////////////////////////////////////////////
// This is the basic primitive used by convex, volumetric lights
// for example spot-lights, one face of the omni lights, etc.
//////////////////////////////////////////////////////////////////////////////////////////
// following options are available to configure compilation:
// USE_LMAP
// USE_LMAPXFORM
// USE_SHADOW
//////////////////////////////////////////////////////////////////////////////////////////

uniform float4              m_lmap        [2]        ;

layout(location = TEXCOORD0) in vec4 tc;
layout(location = TEXCOORD1) in vec4 tcJ;

layout(location = COLOR0) out vec4 oColor;

void main()
{
	float4 _P               = tex2Dproj         (s_position,         tc);
	half4  _N               = tex2Dproj         (s_normal,           tc);

	half 	m	= xmaterial	;
# ifndef USE_R2_STATIC_SUN
			m 	= _P.w		;
# endif
        // ----- light-model
      	half        		rsqr;
        half4        		light   = plight_local( m, _P.xyz, _N.xyz, Ldynamic_pos.xyz, Ldynamic_pos.w, rsqr );

        // ----- shadow
		half4          		P4      = half4( _P.x, _P.y, _P.z, 1);
        float4         		PS      = mul( m_shadow, P4 );
        half         		s		= 1.f;
        #ifdef  USE_SHADOW
                #ifdef         USE_SJITTER
							s		= shadowtest( PS, tcJ );
                #else
							s		= shadow( PS );
                #endif
        #endif

        // ----- lightmap
        half4         lightmap = vec4(1.f);
        #ifdef        USE_LMAP
                #ifdef         USE_LMAPXFORM
							PS.x	= dot( P4, m_lmap[0] );
							PS.y	= dot( P4, m_lmap[1] );
                #endif
                lightmap			= tex2Dproj(s_lmap, PS);        //
				//	Can use linear with mip point
        #endif

        oColor						= blendp ( Ldynamic_color * light * s * lightmap, tc );
}
